package com.creative.config;

import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import com.creative.interceptor.RefreshTokenInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class LoginConfigure implements WebMvcConfigurer {
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    // 注册拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        拦截的路径

        // 注册 Sa-Token 拦截器，校验规则为 StpUtil.checkLogin() 登录校验。
        registry.addInterceptor(new SaInterceptor(handle -> StpUtil.checkLogin()))
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login",
                        "/user/register",
                        "/captcha",
                        "/user/registeEmail",
                        "/user/logout",
                        "/user/resetPassword",
                        "/user/qqLogin",
                        "/XcxUser",
                        "/Banner/mp/list",
                        "/XcxNotice/mp/list",
                        "/MaterialType/mp/list",
                        "/ThematicSelection/mp/list",
                        "/XcxUser/**",
                        "/error",
                        "/Material/mp/randomList/",
                        "/Material/mp/recommend"
                )
                .order(1)
        ;
//        token刷新拦截器
        registry.addInterceptor(new RefreshTokenInterceptor(stringRedisTemplate))
                .addPathPatterns("/**")
                .excludePathPatterns("/user/login",
                        "/user/register",
                        "/captcha",
                        "/user/registeEmail",
                        "/user/logout",
                        "/user/resetPassword",
                        "/user/qqLogin",
                        "/XcxUser",
                        "/XcxUser/**",
                        "/Banner/mp/list",
                        "/XcxNotice/mp/list",
                        "/MaterialType/mp/list",
                        "/ThematicSelection/mp/list",
                        "/error",
                        "/Material/mp/randomList/",
                        "/Material/mp/recommend"
                );
    }
}

